package com.lysj.bhp.exchangerate.controller;

import com.lysj.bhp.exchangerate.base.ExchangeRateContent;
import com.lysj.bhp.exchangerate.domain.ExchangeRate;
import com.lysj.bhp.exchangerate.service.ExchangeRateService;
import com.lysj.bhp.exchangerate.vo.ExchangeRateVo;
import com.lysj.bhp.util.random.FormatUtil;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/exchange_rate")
public class ExchangeRateController extends ExchangeRateContent {

    @Autowired
    private ExchangeRateService exchangeRateService;

    private ExchangeRate exchangeRate = ExchangeRate.getInstance();

    @RequestMapping("list")
    public Resp list(){
        List<ExchangeRateVo> exchangeRateVos = new ArrayList<>();
        exchangeRateService.getRate(RATE_STATUS);
        ExchangeRateVo etherumToDollar = new ExchangeRateVo();
        etherumToDollar.setExchangeRateVoId(1L);
        etherumToDollar.setName("以太币对应美元");
        etherumToDollar.setRate(exchangeRate.getETHEREUM_TO_DOLLAR());
        etherumToDollar.setDateStatus(exchangeRate.getETHEREUM_TO_DOLLAR_STATUS());
        etherumToDollar.setExpires(exchangeRate.getETHEREUM_TO_DOLLAR_EXPIRES());
        etherumToDollar.setIsRate(exchangeRate.getETHEREUM_TO_DOLLAR_ISRATE());
        exchangeRateVos.add(etherumToDollar);

        ExchangeRateVo bitcoinToDollar = new ExchangeRateVo();
        bitcoinToDollar.setExchangeRateVoId(2L);
        bitcoinToDollar.setName("比特币对应美元");
        bitcoinToDollar.setRate(exchangeRate.getBITCOIN_TO_DOLLAR());
        bitcoinToDollar.setDateStatus(exchangeRate.getBITCOIN_TO_DOLLAR_STATUS());
        bitcoinToDollar.setExpires(exchangeRate.getBITCOIN_TO_DOLLAR_EXPIRES());
        bitcoinToDollar.setIsRate(exchangeRate.getBITCOIN_TO_DOLLAR_ISRATE());
        exchangeRateVos.add(bitcoinToDollar);

        ExchangeRateVo bhpcToEth = new ExchangeRateVo();
        bhpcToEth.setExchangeRateVoId(3L);
        bhpcToEth.setName("算力币对以太坊");
        bhpcToEth.setRate(exchangeRate.getBHPC_TO_ETH());
        bhpcToEth.setDateStatus(exchangeRate.getBHPC_TO_ETH_STATUS());
        bhpcToEth.setExpires(exchangeRate.getBHPC_TO_ETH_EXPIRES());
        bhpcToEth.setIsRate(exchangeRate.getBHPC_TO_ETH_ISRATE());
        exchangeRateVos.add(bhpcToEth);

        ExchangeRateVo dollarToCny = new ExchangeRateVo();
        dollarToCny.setExchangeRateVoId(4L);
        dollarToCny.setName("美元兑人民币");
        dollarToCny.setRate(exchangeRate.getDOLLAR_TO_CNY());
        dollarToCny.setDateStatus(exchangeRate.getDOLLAR_TO_CNY_STATUS());
        dollarToCny.setExpires(exchangeRate.getDOLLAR_TO_CNY_EXPIRES());
        dollarToCny.setIsRate(exchangeRate.getDOLLAR_TO_CNY_ISRATE());
        exchangeRateVos.add(dollarToCny);

        ExchangeRateVo bhpcToDollar = new ExchangeRateVo();
        bhpcToDollar.setExchangeRateVoId(5L);
        bhpcToDollar.setName("算力币与美元");
        bhpcToDollar.setRate(exchangeRate.getBHPC_TO_DOLLAR());
        bhpcToDollar.setDateStatus(exchangeRate.getBHPC_TO_DOLLAR_STATUS());
        bhpcToDollar.setExpires(exchangeRate.getBHPC_TO_DOLLAR_EXPIRES());
        bhpcToDollar.setIsRate(exchangeRate.getBHPC_TO_DOLLAR_ISRATE());
        exchangeRateVos.add(bhpcToDollar);

        ExchangeRateVo bhpcToCny = new ExchangeRateVo();
        bhpcToCny.setExchangeRateVoId(6L);
        bhpcToCny.setName("算力币与人民币");
        bhpcToCny.setRate(exchangeRate.getBHPC_TO_CNY());
        bhpcToCny.setDateStatus(exchangeRate.getBHPC_TO_CNY_STATUS());
        bhpcToCny.setExpires(exchangeRate.getBHPC_TO_CNY_EXPIRES());
        bhpcToCny.setIsRate(exchangeRate.getBHPC_TO_CNY_ISRATE());
        exchangeRateVos.add(bhpcToCny);

        ExchangeRateVo cnyToDollar = new ExchangeRateVo();
        cnyToDollar.setExchangeRateVoId(7L);
        cnyToDollar.setName("人民币与美元");
        cnyToDollar.setRate(exchangeRate.getCNY_TO_DOLLAR());
        cnyToDollar.setDateStatus(exchangeRate.getCNY_TO_DOLLAR_STATUS());
        cnyToDollar.setExpires(exchangeRate.getCNY_TO_DOLLAR_EXPIRES());
        cnyToDollar.setIsRate(exchangeRate.getCNY_TO_DOLLAR_ISRATE());
        exchangeRateVos.add(cnyToDollar);

        ExchangeRateVo bitcoinToCny = new ExchangeRateVo();
        bitcoinToCny.setExchangeRateVoId(8L);
        bitcoinToCny.setName("比特币对人民币");
        bitcoinToCny.setRate(exchangeRate.getBITCOIN_TO_CNY());
        bitcoinToCny.setDateStatus(exchangeRate.getBITCOIN_TO_CNY_STATUS());
        bitcoinToCny.setExpires(exchangeRate.getBITCOIN_TO_CNY_EXPIRES());
        bitcoinToCny.setIsRate(exchangeRate.getBITCOIN_TO_CNY_ISRATE());
        exchangeRateVos.add(bitcoinToCny);

        ExchangeRateVo ethToCny = new ExchangeRateVo();
        ethToCny.setExchangeRateVoId(9L);
        ethToCny.setName("以太坊对人民币");
        ethToCny.setRate(exchangeRate.getETH_TO_CNY());
        ethToCny.setDateStatus(exchangeRate.getETH_TO_CNY_STATUS());
        ethToCny.setExpires(exchangeRate.getETH_TO_CNY_EXPIRES());
        ethToCny.setIsRate(exchangeRate.getETH_TO_CNY_ISRATE());
        exchangeRateVos.add(ethToCny);

        return new Resp(exchangeRateVos);
    }

    @PostMapping("edit")
    public Resp edit(BigDecimal rate, String expires, Long exchangeRateVoId) throws Exception {
        if(rate.compareTo(new BigDecimal(BigInteger.ZERO)) != 1){
            return new Resp(Resp.PARAM_ERROR,"基础汇率大于0");
        }
        if(ParamUtil.isBlack(expires)){
            return new Resp(Resp.PARAM_ERROR,"请输入缓存有效时间");
        }
        long date = FormatUtil.stringTodates(expires).getTime();
        if(exchangeRateVoId == 1){
            ETHEREUM_TO_DOLLAR_ISRATE = 2;
            exchangeRate.setETHEREUM_TO_DOLLAR_EXPIRES(date);
            exchangeRate.setETHEREUM_TO_DOLLAR(rate);
        }else if(exchangeRateVoId == 2){
            BITCOIN_TO_DOLLAR_ISRATE = 2;
            exchangeRate.setBITCOIN_TO_DOLLAR_EXPIRES(date);
            exchangeRate.setBITCOIN_TO_DOLLAR(rate);
        }else if(exchangeRateVoId == 3){
            BHPC_TO_ETH_ISRATE = 2;
            exchangeRate.setBHPC_TO_ETH_EXPIRES(date);
            exchangeRate.setBHPC_TO_ETH(rate);
        }else if(exchangeRateVoId == 4){
            DOLLAR_TO_CNY_ISRATE = 2;
            exchangeRate.setDOLLAR_TO_CNY_EXPIRES(date);
            exchangeRate.setDOLLAR_TO_CNY(rate);
        }else if(exchangeRateVoId == 5){
            BHPC_TO_DOLLAR_ISRATE = 2;
            exchangeRate.setBHPC_TO_DOLLAR_EXPIRES(date);
            exchangeRate.setBHPC_TO_DOLLAR(rate);
        }else if(exchangeRateVoId == 6){
            BHPC_TO_CNY_ISRATE = 2;
            exchangeRate.setBHPC_TO_CNY_EXPIRES(date);
            exchangeRate.setBHPC_TO_CNY(rate);
        }else if(exchangeRateVoId == 7){
            CNY_TO_DOLLAR_ISRATE = 2;
            exchangeRate.setCNY_TO_DOLLAR_EXPIRES(date);
            exchangeRate.setCNY_TO_DOLLAR(rate);
        }else if(exchangeRateVoId == 8){
            BITCOIN_TO_CNY_ISRATE = 2;
            exchangeRate.setBITCOIN_TO_CNY_EXPIRES(date);
            exchangeRate.setBITCOIN_TO_CNY(rate);
        }else if(exchangeRateVoId == 9){
            ETH_TO_CNY_ISRATE = 2;
            exchangeRate.setETH_TO_CNY_EXPIRES(date);
            exchangeRate.setETH_TO_CNY(rate);
        }
        exchangeRateService.getRate(RATE_STATUS);
        return new Resp("修改成功");
    }

    @PostMapping("/editData")
    public Resp editData(ExchangeRateVo model){
        ETHEREUM_TO_DOLLAR_ISRATE = 1;
        BITCOIN_TO_DOLLAR_ISRATE = 1;
        BHPC_TO_ETH_ISRATE = 1;
        DOLLAR_TO_CNY_ISRATE = 1;
        RATE_STATUS = model.getDateStatus();
        exchangeRate.setETHEREUM_TO_DOLLAR_STATUS(RATE_STATUS);
        exchangeRate.setBITCOIN_TO_DOLLAR_STATUS(RATE_STATUS);
        exchangeRate.setBHPC_TO_ETH_STATUS(RATE_STATUS);
        exchangeRate.setDOLLAR_TO_CNY_STATUS(RATE_STATUS);
        exchangeRate.setBHPC_TO_DOLLAR_STATUS(RATE_STATUS);
        exchangeRate.setBHPC_TO_CNY_STATUS(RATE_STATUS);
        exchangeRate.setCNY_TO_DOLLAR_STATUS(RATE_STATUS);
        exchangeRate.setBITCOIN_TO_CNY_STATUS(RATE_STATUS);
        exchangeRate.setETH_TO_CNY_STATUS(RATE_STATUS);
        exchangeRateService.getRate(RATE_STATUS);
        return new Resp("修改成功");
    }

    @GetMapping("rates")
    public Resp rates(){
        return new Resp(ExchangeRate.getInstance());
    }


    @ApiOperation(value = "以太币到美元的汇率", notes = "")
    @PostMapping("/ethereum_to_dollar_rate")
    public Resp ethereumToDollarRate() throws Exception {
        BigDecimal ethereumToDollarRate = exchangeRate.getETHEREUM_TO_DOLLAR();
        return new Resp(Resp.SUCCESS, "success", ethereumToDollarRate);
    }

    @ApiOperation(value = "比特币到美元的汇率", notes = "")
    @PostMapping("/bitcoin_to_dollar_rate")
    public Resp bitcoinToDollarRate() throws Exception {
        BigDecimal bitcoinToDollarRate = exchangeRate.getBITCOIN_TO_DOLLAR();
        return new Resp(Resp.SUCCESS, "success", bitcoinToDollarRate);
    }

    @ApiOperation(value = "算力币到美元的汇率", notes = "")
    @PostMapping("/bhpc_to_dollar_rate")
    public Resp bhpcToDollarRate() throws Exception {
        BigDecimal bitcoinToDollarRate = exchangeRateService.getBhpcToDollar();
        return new Resp(Resp.SUCCESS, "success", bitcoinToDollarRate);
    }

    @ApiOperation(value = "算力币到人民币的汇率", notes = "")
    @PostMapping("/bhpc_to_cny_rate")
    public Resp bhpcToCnyRate() throws Exception {
        BigDecimal bhpcToCnyRate = exchangeRateService.bhpcToCnyRate();
        return new Resp(Resp.SUCCESS, "success", bhpcToCnyRate);
    }

    @ApiOperation(value = "美元到人民币的汇率", notes = "")
    @PostMapping("/dollar_to_cny_rate")
    public Resp dollarToCnyRate() throws Exception {
        BigDecimal dolarToCnyRate = exchangeRate.getDOLLAR_TO_CNY();
        return new Resp(Resp.SUCCESS, "success", dolarToCnyRate);
    }
}
